#
# @lc app=leetcode.cn id=205 lang=python3
#
# [205] 同构字符串
#

# @lc code=start
class Solution:
    def isIsomorphic(self, s: str, t: str) -> bool:
        # # 法1(比较呆)
        # hashMap1, hashMap2 = {}, {}
        # seen1, seen2 = set(), set()
        # for index1, c in enumerate(s):
        #     if c not in seen1:
        #         hashMap1[c] = [index1]
        #         seen1.add(c)
        #     else:
        #         hashMap1[c].append(index1)
        # for index2, d in enumerate(t):
        #     if d not in seen2:
        #         hashMap2[d] = [index2]
        #         seen2.add(d)
        #     else:
        #         hashMap2[d].append(index2)
        # if len(hashMap1) != len(hashMap2): return False
        # res1 = [hashMap1[key] for key in hashMap1.keys()]
        # res2 = [hashMap2[key] for key in hashMap2.keys()]
        # return res1 == res2
        # 法2
        s2t = {}
        seen = set()
        for i in range(len(s)):
            # 键和值必须一一映射上
            if s[i] in s2t:
                if s2t[s[i]] != t[i]: return False
            else:
                # 若键不在映射字典中，而对应的值却已出现过，说明键和值不是一一映射
                if t[i] in seen: return False
                seen.add(t[i])
                # 把映射加进字典
                s2t[s[i]] = t[i]
        return True
            
            


# @lc code=end

